//
//  NSLog+Console.m
//  SmartPigai
//
//  Created by mac on 16/10/29.
//  Copyright © 2016年 SmartStudy. All rights reserved.
//

#import "NSLog+Console.h"

#pragma mark -  NSLog打印辅助方法
id logExtension(id obj) {
  id tempObj = obj;
  // 遇到NSArray、NSSet或NSDictionary的子类，内容后移\t
  if ([obj isKindOfClass:[NSDictionary class]] || [obj isKindOfClass:[NSArray class]] || [obj isKindOfClass:[NSSet class]]) {
    NSString *str = [NSString stringWithFormat:@"%@", obj];
    str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\t"];
    tempObj = str;
  } else if ([obj isKindOfClass:[NSString class]]) { // NSString类型数据加双引号
    tempObj = [NSString stringWithFormat:@"\"%@\"", obj];
  }
  return tempObj;
}

#pragma mark - 数组NSLog打印扩展
@implementation NSArray (Console)

#pragma mark 数组打印
- (NSString *)descriptionWithLocale:(id)locale {
  NSMutableString *str = [NSMutableString stringWithString:@"(\n"];
  // 遍历数组的所有元素
  for (id obj in self) {
    [str appendFormat:@"\t%@,\n", logExtension(obj)];
  }
  [str appendString:@")"];
  return str;
}

- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    NSMutableString *desc = [NSMutableString string];
    NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level];
    for (NSUInteger i = 0; i < level; ++i) {
        [tabString appendString:@"\t"];
    }
    NSString *tab = @"";
    if (level > 0) {
        tab = tabString;
    }
    [desc appendString:@"\t(\n"];
    for (id obj in self) {
        if ([obj isKindOfClass:[NSDictionary class]]
            || [obj isKindOfClass:[NSArray class]]
            || [obj isKindOfClass:[NSSet class]]) {
            NSString *str = [((NSDictionary *)obj) descriptionWithLocale:locale indent:level + 1];
            [desc appendFormat:@"%@\t%@,\n", tab, str];
        } else if ([obj isKindOfClass:[NSString class]]) {
            [desc appendFormat:@"%@\t\"%@\",\n", tab, obj];
        } else if ([obj isKindOfClass:[NSData class]]) {
            NSError *error = nil;
            NSObject *result = [NSJSONSerialization JSONObjectWithData:obj
                                                               options:NSJSONReadingMutableContainers
                                                                 error:&error];
            if (error == nil && result != nil) {
                if ([result isKindOfClass:[NSDictionary class]]
                    || [result isKindOfClass:[NSArray class]]
                    || [result isKindOfClass:[NSSet class]]) {
                    NSString *str = [((NSDictionary *)result) descriptionWithLocale:locale indent:level + 1];
                    [desc appendFormat:@"%@\t%@,\n", tab, str];
                } else if ([obj isKindOfClass:[NSString class]]) {
                    [desc appendFormat:@"%@\t\"%@\",\n", tab, result];
                }
            } else {
                @try {
                    NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
                    if (str != nil) {
                        [desc appendFormat:@"%@\t\"%@\",\n", tab, str];
                    } else {
                        [desc appendFormat:@"%@\t%@,\n", tab, obj];
                    }
                }
                @catch (NSException *exception) {
                    [desc appendFormat:@"%@\t%@,\n", tab, obj];
                }
            }
        } else {
            [desc appendFormat:@"%@\t%@,\n", tab, obj];
        }
    }
    [desc appendFormat:@"%@)", tab];
    return desc;
}

@end

#pragma mark - 字典NSLog打印扩展
@implementation NSDictionary (Console)

#pragma mark 字典打印
- (NSString *)descriptionWithLocale:(id)locale {
  __block NSMutableString *str = [NSMutableString stringWithString:@"{\n"];
  // 遍历字典的所有键值对
  [self enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
    [str appendFormat:@"\t%@ = %@,\n", key, logExtension(obj)];
  }];
  [str appendString:@"}"];
  // 删掉最后一个,
  NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];
  // 保护机制找到才删除
  if (range.location > 0 && range.location < str.length) {
    [str deleteCharactersInRange:range];
  }
  return str;
}

- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    NSMutableString *desc = [NSMutableString string];
    NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level];
    for (NSUInteger i = 0; i < level; ++i) {
        [tabString appendString:@"\t"];
    }
    NSString *tab = @"";
    if (level > 0) {
        tab = tabString;
    }
    [desc appendString:@"\t{\n"];
    // Through array, self is array
    for (id key in self.allKeys) {
        id obj = [self objectForKey:key];
        if ([obj isKindOfClass:[NSString class]]) {
            [desc appendFormat:@"%@\t%@ = \"%@\",\n", tab, key, obj];
        } else if ([obj isKindOfClass:[NSArray class]]
                   || [obj isKindOfClass:[NSDictionary class]]
                   || [obj isKindOfClass:[NSSet class]]) {
            [desc appendFormat:@"%@\t%@ = %@,\n", tab, key, [obj descriptionWithLocale:locale indent:level + 1]];
        } else if ([obj isKindOfClass:[NSData class]]) {
            NSError *error = nil;
            NSObject *result = [NSJSONSerialization JSONObjectWithData:obj
                                                               options:NSJSONReadingMutableContainers
                                                                 error:&error];
            if (error == nil && result != nil) {
                if ([result isKindOfClass:[NSDictionary class]]
                    || [result isKindOfClass:[NSArray class]]
                    || [result isKindOfClass:[NSSet class]]) {
                    NSString *str = [((NSDictionary *)result) descriptionWithLocale:locale indent:level + 1];
                    [desc appendFormat:@"%@\t%@ = %@,\n", tab, key, str];
                } else if ([obj isKindOfClass:[NSString class]]) {
                    [desc appendFormat:@"%@\t%@ = \"%@\",\n", tab, key, result];
                }
            } else {
                @try {
                    NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
                    if (str != nil) {
                        [desc appendFormat:@"%@\t%@ = \"%@\",\n", tab, key, str];
                    } else {
                        [desc appendFormat:@"%@\t%@ = %@,\n", tab, key, obj];
                    }
                }
                @catch (NSException *exception) {
                    [desc appendFormat:@"%@\t%@ = %@,\n", tab, key, obj];
                }
            }
        } else {
            [desc appendFormat:@"%@\t%@ = %@,\n", tab, key, obj];
        }
    }
    [desc appendFormat:@"%@}", tab];
    return desc;
}

@end

#pragma mark - NSSet NSLog打印扩展
@implementation NSSet (Console)

#pragma mark NSSet打印
- (NSString *)descriptionWithLocale:(id)locale {
  NSMutableString *str = [NSMutableString stringWithString:@"{(\n"];
  for (id value in self) {
    [str appendFormat:@"\t%@,\n", logExtension(value)];
  }
  [str appendString:@")}"];
  // 删掉最后一个,
  NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];
  // 保护机制找到才删除
  if (range.location > 0 && range.location < str.length) {
    [str deleteCharactersInRange:range];
  }
  return str;
}
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
    NSMutableString *desc = [NSMutableString string];
    NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level];
    for (NSUInteger i = 0; i < level; ++i) {
        [tabString appendString:@"\t"];
    }
    
    NSString *tab = @"\t";
    if (level > 0) {
        tab = tabString;
    }
    [desc appendString:@"\t{(\n"];
    
    for (id obj in self) {
        if ([obj isKindOfClass:[NSDictionary class]]
            || [obj isKindOfClass:[NSArray class]]
            || [obj isKindOfClass:[NSSet class]]) {
            NSString *str = [((NSDictionary *)obj) descriptionWithLocale:locale indent:level + 1];
            [desc appendFormat:@"%@\t%@,\n", tab, str];
        } else if ([obj isKindOfClass:[NSString class]]) {
            [desc appendFormat:@"%@\t\"%@\",\n", tab, obj];
        } else if ([obj isKindOfClass:[NSData class]]) {
            // if is NSData，try parse
            NSError *error = nil;
            NSObject *result = [NSJSONSerialization JSONObjectWithData:obj
                                                               options:NSJSONReadingMutableContainers
                                                                 error:&error];
            if (error == nil && result != nil) {
                if ([result isKindOfClass:[NSDictionary class]]
                    || [result isKindOfClass:[NSArray class]]
                    || [result isKindOfClass:[NSSet class]]) {
                    NSString *str = [((NSDictionary *)result) descriptionWithLocale:locale indent:level + 1];
                    [desc appendFormat:@"%@\t%@,\n", tab, str];
                } else if ([obj isKindOfClass:[NSString class]]) {
                    [desc appendFormat:@"%@\t\"%@\",\n", tab, result];
                }
            } else {
                @try {
                    NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding];
                    if (str != nil) {
                        [desc appendFormat:@"%@\t\"%@\",\n", tab, str];
                    } else {
                        [desc appendFormat:@"%@\t%@,\n", tab, obj];
                    }
                }
                @catch (NSException *exception) {
                    [desc appendFormat:@"%@\t%@,\n", tab, obj];
                }
            }
        } else {
            [desc appendFormat:@"%@\t%@,\n", tab, obj];
        }
    }
    [desc appendFormat:@"%@)}", tab];
     return desc;
}
@end
